package it.unisa.services;

import it.unisa.beans.Account;
import it.unisa.beans.Comment;
import it.unisa.beans.Contour;
import it.unisa.beans.Petroglyph;
import it.unisa.beans.Photo;
import it.unisa.dao.ContourDao;
import it.unisa.dao.PhotoDao;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.sql.Date;
import java.util.GregorianCalendar;
import java.util.List;
import org.apache.commons.codec.binary.Base64;


import java.sql.Time;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * TODO method remove(int id)	metodo invocabili da owner e moderazione
 * TODO method update(Photo p) 	metodo invocabili da owner e moderazione
 * 
 * @author Alfonso
 *
 */
public class PhotoWS {
	
	public boolean addPicture(Photo photo, String base64photo,String base64Contour,Contour contour) {
		ApplicationContext appContext = new ClassPathXmlApplicationContext("beans.xml");
		PhotoDao photoDao=(PhotoDao)appContext.getBean("photoDao");
		try {
			GregorianCalendar gc=new GregorianCalendar();
			GregorianCalendar today=new GregorianCalendar();
			if(base64Contour!=null && contour!=null){
				System.out.println("bla bla bli");
				byte[] btDataFile=new sun.misc.BASE64Decoder().decodeBuffer(base64Contour);
				File of=new File("contour/"+today.getTimeInMillis()+"/"+photo.getTitle()+".jpg");
				of.getParentFile().mkdirs();
				FileOutputStream osf = new FileOutputStream(of);
				osf.write(btDataFile);
				osf.flush();
				contour.setUrl_contour(of.getAbsolutePath());
			}			
			System.out.println("base:"+base64photo);

			byte[] btDataFile = new sun.misc.BASE64Decoder().decodeBuffer(base64photo);	
			Date date=new Date(today.getTimeInMillis());
			Time time=new Time(today.getTimeInMillis());
			photo.setDate(date);
			photo.setHour(time);
			
			File of = new File("photos/"+today.getTimeInMillis()+"/"+photo.getTitle()+".jpg");
			of.getParentFile().mkdirs();
			FileOutputStream osf = new FileOutputStream(of);
			osf.write(btDataFile);
			osf.flush();
			photo.setUrl(of.getAbsolutePath());
			//photo.setContour(contour);
			photoDao.insert(photo);
			if (base64Contour != null && contour != null) {
				List<Photo> list = photoDao.find(photo);
				if (list.get(0) != null) {
					contour.setDate(date);
					contour.setHour(time);
					Photo p = list.get(0);
					p.setId(list.get(0).getId());
					contour.setPhoto(p);
					ContourDao contourDao = (ContourDao) appContext
							.getBean("contourDao");
					contourDao.insert(contour);
					p.setContour(contour);
					photoDao.update(p);
					return true;

				} else
					return false;
			}
			else
				return true;
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		}
		
		
	}
	
	
	public it.unisa.beanService.Photo[] getAllPictures() {
		ApplicationContext appContext = new ClassPathXmlApplicationContext("beans.xml");
		PhotoDao photo=(PhotoDao)appContext.getBean("photoDao");
		List<Photo> foto = photo.findAllPhotos();
		it.unisa.beanService.Photo[] resp = new it.unisa.beanService.Photo[foto.size()];
		for(int i=0; i<foto.size(); i++) {
			Photo r = foto.get(i);
			it.unisa.beanService.Photo p = new it.unisa.beanService.Photo();
			p.setId(r.getId());
			p.setTitle(r.getTitle());
			p.setDescription(r.getDescription());
			p.setDate(r.getDate().toString());
			p.setHour(r.getHour().toString());
			p.setId_account(r.getAccount().getSsn());
			if(r.getPetroglyph()!=null)
				p.setId_petroglyph(r.getPetroglyph().getId());
			p.setPositionX(r.getPositionX());
			p.setPositionY(r.getPositionY());
			p.setUrl(r.getUrl());
			int[] comm = new int[r.getComments().size()];
			int j=0;
			for(Comment co : r.getComments()) {
				comm[j] = co.getId();
			}
			resp[i] = p;
		}
		return resp;
	}
		
	public it.unisa.beanService.Photo[] getPhotoByAccount(String id_account) {
		ApplicationContext appContext = new ClassPathXmlApplicationContext("beans.xml");
		PhotoDao photo=(PhotoDao)appContext.getBean("photoDao");
		Account a = new Account();
		a.setSsn(id_account);
		Photo ph = new Photo();
		ph.setAccount(a);
		List<Photo> foto =  photo.findAllPhotos();
		it.unisa.beanService.Photo[] resp = new it.unisa.beanService.Photo[foto.size()];
		int k = 0;
		for(int i=0; i<foto.size(); i++) {
			Photo r = foto.get(i);
			if(r.getAccount()==null || !r.getAccount().getSsn().equals(id_account) ){
				continue;
			}
			it.unisa.beanService.Photo p = new it.unisa.beanService.Photo();
			p.setId(r.getId());
			p.setTitle(r.getTitle());
			p.setDescription(r.getDescription());
			p.setDate(r.getDate().toString());
			p.setHour(r.getHour().toString());
			p.setId_account(r.getAccount().getSsn());
			if(r.getPetroglyph()!=null)
				p.setId_petroglyph(r.getPetroglyph().getId());
			p.setPositionX(r.getPositionX());
			p.setPositionY(r.getPositionY());
			p.setUrl(r.getUrl());
			int[] comm = new int[r.getComments().size()];
			int j=0;
			for(Comment co : r.getComments()) {
				comm[j] = co.getId();
			}
			resp[i] = p;
		}
		return resp;


	}
	
	public it.unisa.beanService.Photo[] getPhotoByPetroglyph(int id_petroglyph) {
		ApplicationContext appContext = new ClassPathXmlApplicationContext("beans.xml");
		PhotoDao photo=(PhotoDao)appContext.getBean("photoDao");
		Photo ph = new Photo();
		Petroglyph pt = new Petroglyph();
		pt.setId(id_petroglyph);
		ph.setPetroglyph(pt);
		List<Photo> foto =  photo.findAllPhotos();
		it.unisa.beanService.Photo[] resp = new it.unisa.beanService.Photo[foto.size()];
		int k = 0;
		for(int i=0; i<foto.size(); i++) {
			Photo r = foto.get(i);
			if(r.getPetroglyph()==null || r.getPetroglyph().getId()!=id_petroglyph ){
				continue;
			}
			it.unisa.beanService.Photo p = new it.unisa.beanService.Photo();
			p.setId(r.getId());
			p.setTitle(r.getTitle());
			p.setDescription(r.getDescription());
			p.setDate(r.getDate().toString());
			p.setHour(r.getHour().toString());
			p.setId_account(r.getAccount().getSsn());
			if(r.getPetroglyph()!=null)
				p.setId_petroglyph(r.getPetroglyph().getId());
			p.setPositionX(r.getPositionX());
			p.setPositionY(r.getPositionY());
			p.setUrl(r.getUrl());
			int[] comm = new int[r.getComments().size()];
			int j=0;
			for(Comment co : r.getComments()) {
				comm[j] = co.getId();
			}
			resp[k] = p;
			k++;
		}
		it.unisa.beanService.Photo[] response = java.util.Arrays.copyOf(resp, k);
		return response;

	}
	
	public it.unisa.beanService.Photo getPhotoByURL(String url) {

		
		ApplicationContext appContext = new ClassPathXmlApplicationContext("beans.xml");
		PhotoDao photo=(PhotoDao)appContext.getBean("photoDao");
		Photo r = new Photo();
		r.setUrl(url);
		List<Photo> foto =  photo.find(r);
		r = foto.get(0);
		it.unisa.beanService.Photo p = new it.unisa.beanService.Photo();
		p.setId(r.getId());
		p.setTitle(r.getTitle());
		p.setDescription(r.getDescription());
		p.setDate(r.getDate().toString());
		p.setHour(r.getHour().toString());
		p.setId_account(r.getAccount().getSsn());
		if(r.getPetroglyph()!=null)
			p.setId_petroglyph(r.getPetroglyph().getId());
		p.setPositionX(r.getPositionX());
		p.setPositionY(r.getPositionY());
		p.setUrl(r.getUrl());
		int[] comm = new int[r.getComments().size()];
		int j=0;
		for(Comment co : r.getComments()) {
			comm[j] = co.getId();
		}
		return p;
	}

	public boolean removePhoto(int idPhoto){
		ApplicationContext appContext = new ClassPathXmlApplicationContext("beans.xml");
		PhotoDao photoDao=(PhotoDao)appContext.getBean("photoDao");
		photoDao.delete(idPhoto);
		return true;
	}

	public boolean updatePhoto(Photo photo){
		ApplicationContext appContext = new ClassPathXmlApplicationContext("beans.xml");
		PhotoDao photoDao=(PhotoDao)appContext.getBean("photoDao");
		photoDao.update(photo);
		return true;
	}
	public static void main(String[] args){
		
	}
}
